<?php

class LikerModel extends SCModel {
	
	/**
	 * 
	 * 设置thing like
	 * @param integer $thing_id
	 * @param integer $user_id
	 * @return boolean
	 */
	public function setThingLike($thing_id, $user_id = 0) {
		if($user_id) { // 登录用户
			$condition = 'related_id = '.$thing_id.' AND type = "'.ThingModel::translateType('thing').'" AND user_id = '.$user_id;
			if($this->from('liker')->where($condition)->find()) return false; // 已经like过
			
			$this->from('liker')->add(array(
				'related_id' => $thing_id,
				'type' => ThingModel::translateType('thing'),
				'user_id' => $user_id
			));
			return true;
			
		} else { // 游客
			$ip = CommonFunction::getClientIP();
			$condition = 'related_id = '.$thing_id.' AND type = '.ThingModel::translateType('thing').' AND ip = "'.$ip.'"';
			if($this->from('liker')->where($condition)->find())  return false; // 已经like过
			
			$this->from('liker')->add(array(
				'related_id' => $thing_id,
				'type' => ThingModel::translateType('thing'),
				'ip' => $ip
			));
			return true;
		}
	}
} 